﻿using System;
using System.Collections.Generic;
using System.Text;
using Eis51.CampanyCMS.Model;
using System.Data.OleDb;
using System.Data;
using System.Configuration;

namespace MoreUser.Bussiness
{
    public   class BaseSearchConditonBLL
    {
        public static  string GetConditon()
        {
            string condtion = " Createby='{0}' and licd={1}";
            if (AppContext.GetBaseSearchConditon != null)
            {
                condtion = string.Format(condtion, AppContext.GetBaseSearchConditon.CreateBy, AppContext.GetBaseSearchConditon.licd);
            }
            else
            {
                condtion = string.Format(condtion, "@@@@@", 0);
            }
            return condtion;
        }

        public static string GetConditon(bool IsAnd)
        {
            string condtion = " Createby='{0}' and licd={1}";
            if (IsAnd)
            {
                condtion = " and " + condtion;
            }
            if (AppContext.GetBaseSearchConditon != null)
            {
                condtion = string.Format(condtion, AppContext.GetBaseSearchConditon.CreateBy, AppContext.GetBaseSearchConditon.licd);
            }
            else
            {
                condtion = string.Format(condtion, "@@@@@", 0);
            }
            return condtion;
        }

        public static string GetConditon(string sql, bool IsAnd)
        {

            string condtion = " Createby='{0}' and licd={1}";
            if (IsAnd)
            {
                condtion = " and " + condtion;
            }
            if (AppContext.GetBaseSearchConditon != null)
            {
                condtion = string.Format(condtion, AppContext.GetBaseSearchConditon.CreateBy, AppContext.GetBaseSearchConditon.licd);
            }
            else
            {
                condtion = string.Format(condtion, "@@@@@", 0);
            }
            return sql+condtion;
        }


        /// <summary>
        /// 执行查询语句，返回分页的DATATABLE
        /// </summary>
        /// <param name="SQLString">查询语句</param>
        /// <returns>DataSet</returns>
        public static DataTable GetDataForPage(string SQLString, int first, int pagesize)
        {
            string connectionString = string.Format(ConfigurationSettings.AppSettings["Eis_ConnectionString"], System.Web.HttpContext.Current.Request.PhysicalApplicationPath);

            using (OleDbConnection connection = new OleDbConnection(connectionString))
            {
                DataSet ds = new DataSet();
                try
                {
                    connection.Open();
                    OleDbDataAdapter dataAdapter = new OleDbDataAdapter(SQLString, connection);
                    int min = (first - 1) * pagesize ;
                    int max = first * pagesize;
                    dataAdapter.Fill(ds, min, max, "output");
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
                return ds.Tables["output"];
            }
        }

    }
}
